// pages/login/login.js
import request from '../../utils/request'
Page({
  wxLogin(e) {
    // console.log(e);
    // 获取用户的昵称和头像
    if (e.detail.errMsg === "getUserInfo:fail auth deny") return
    const { nickName: nickname, avatarUrl: avatar } = e.detail.userInfo
    // console.log(nickname, avatar);
    wx.login({
      // 获取临时登录的code
      success: async ({ code }) => {
        // console.log(code);
        const res = await request({ url: 'user/wxlogin', method: 'POST', data: { nickname, avatar, code }, tip: '微信授权登录中......' })
        // console.log(res, 111)
        if (res.data.status === 0) {
          wx.setStorageSync('token', res.data.token)
          wx.showToast({
            title: 'res.data.message',
            icon: 'none',
            duration: 1500,
            success: () => {
              wx.reLaunch({
                url: '/pages/home/home',
              })
            }
          })
        }
      }
    })

  },
  wxPhoneLogin(){
    wx.navigateTo({
      url: '/pages/phone-login/phone-login',
    })
  }

})